# !/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@author: JHC000abc@gmail.com
@file: crawl_tonghuashun.py
@time: 2025/3/13 14:19 
@desc: 

"""
import os
import atexit
from datetime import datetime
from base import Base


class CrawlTongHuaShun(Base):
    """

    """

    def __init__(self, jgbsessid):
        super().__init__()
        atexit.register(self.exit_handler)
        self.org_id_url = "https://ft.51ifind.com/standardgwapi/api/company_library/keyword/new/suggest?keyword={searchKey}"
        self.org_code_url = "https://ft.51ifind.com/standardgwapi/api/company_library/company_detail/query_invoice_information?orgId={orgId}"
        self.detail_url = "https://ft.51ifind.com/standardgwapi/api/news_service/announcement/index/list"

        self.platform = "同花顺"
        self.result_tmp_jsonl = f"result_tmp_{self.platform}_{datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}.jsonl"
        self.thread_num = 5
        self.sleep_time = 15

        self.cookies = {
            "jgbsessid": f"{jgbsessid}",
        }
        # 启动前先记录上次的 历史记录 方便误操作后恢复
        self.start_recode_history()

    def getOrgid(self, searchKey):
        """

        :param searchKey:
        :return:
        """
        response = self.requests(self.org_id_url.format(searchKey=searchKey), headers=self.headers,
                                 cookies=self.cookies, method="GET")
        if response:
            if self.check_response(response):
                data = response.json()
                for item in data["data"]:
                    companyName = item["companyName"]
                    orgid = item["orgid"]
                    if companyName == searchKey:
                        return orgid
        if response.status_code == 401:
            return -1
        return None

    def getOrganizationCode(self, orgid, searchKey):
        """

        :return:
        """
        response = self.requests(self.org_code_url.format(orgId=orgid), headers=self.headers, cookies=self.cookies)
        if response:
            if self.check_response(response):
                data = response.json()["data"]
                items = data["items"]
                for item in items:
                    companyInvoiceName = item["companyInvoiceName"]
                    taxRegNum = item["taxRegNum"]
                    if companyInvoiceName == searchKey:
                        return taxRegNum
        return None

    def get_detail(self, searchKey, orgid):
        """

        :param searchKey:
        :param orgid:
        :return:
        """
        data = {
            "beginDate": "2022-12-31",
            "esp": 1,
            "keyword": f"{searchKey}",
            "page": 1,
            "limit": 500,
            "nodeidArr": "[{\"nodeid\":\"903\",\"children\":[]}]",
            "type": 1,
        }
        response = self.requests(self.detail_url, headers=self.headers, cookies=self.cookies, json_data=data)
        if response:
            if self.check_response(response):
                data = response.json()["data"]
                lis = data["list"]
                for li in lis:
                    companyCode = li["companyCode"]
                    if companyCode == orgid:
                        title = li["title"].replace("^", "")
                        originalUrl = li["originalUrl"]
                        yield title, originalUrl

    def thread(self, company_name, origin_id, save_folder, index):
        """

        :param company_name:
        :param origin_id:
        :param save_folder:
        :return:
        """
        res = self.get_results()
        res["origin"].update(
            {
                "company_name": company_name,
                "origin_id": origin_id,
                "index": index,
                "data_from": "同花顺"
            }
        )
        orgid = self.getOrgid(company_name)
        if orgid is None:
            res["msg"] = "未获取到 orgid"
            res["time"] = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
            return res

        if orgid == -1:
            res["msg"] = "cookies 失效"
            res["time"] = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
            return res
        file_name_recode_set = set()
        for title, originalUrl in self.get_detail(company_name, orgid):
            pdf_status, pdf_type = self.check_pdf_name(title)
            tmp = {originalUrl: {
                "title": title,
                "status": pdf_status,
                "type": pdf_type,
            }}
            if not pdf_status:
                res["failed"].update(tmp)
            else:
                pdf_save_file, file_name_recode_set = self.check_file_name(title, company_name,
                                                                           file_name_recode_set, saveFolder=save_folder)
                if pdf_save_file is not None:
                    # self.download_pdf(originalUrl, self.headers, pdf_save_file)
                    tmp[originalUrl].update({"save_file": pdf_save_file})
                    res["results"].append([{self.type_map.get(pdf_type, "default"): pdf_save_file}])
                    res["success"].update(tmp)
        res["status"] = 0
        res["msg"] = "检查到有效数据" if len(res["success"]) > 0 or len(res["failed"]) > 0 else "未检查到有效数据"
        res["time"] = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
        return res

    def process(self, *args, **kwargs):
        """

        :param args:
        :param kwargs:
        :return:
        """
        file = kwargs["file"]
        save_folder = f"./{self.platform}"
        os.makedirs(save_folder, exist_ok=True)
        index = 0
        for company_name, origin_id in self.get_origin_company_info(file):
            index += 1
            self.pool.add_1job_with_callback(self.callback, self.thread, company_name, origin_id, save_folder, index)
        self.pool.stop()
        self.clear_tmp_result(self.result_tmp_jsonl)
        print(f"所有任务处理完成,整理结果参见：{self.result_tmp_jsonl},文件存储在:{save_folder}")
        return self.result_tmp_jsonl


if __name__ == '__main__':
    # file = r"D:\Desktop\采集主体 (1) (1) - 副本.xlsx"
    file = input("输入同花顺采集主体文件路径:")
    date_time = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
    # save_folder = rf"D:\Desktop\6\tmp_同花顺_{date_time}"
    jgbsessid = "6f4d391a0073b70741017930abdcc7f6"
    CrawlTongHuaShun(jgbsessid).process(file=file)
